<template>
  <div>
    <h1>人员列表</h1>
    <h3 style="color:red">Couont组件的求和是：{{sum}}</h3>
    <input type="text" v-model="name" placeholder="请输入名字">
    <button @click="add">添加</button>
    <ul>
        <li v-for="p in personList" :key="p.id">{{p.name}}</li>
    </ul>
  </div>
</template>

<script>
import {nanoid} from 'nanoid'
import {mapState, mapGetters} from 'vuex'
import {mapActions, mapMutations} from 'vuex'
export default {
    name: 'Person',
    data() {
        return {
            name: '' //输入的名字
        }
    },
    computed: {
        //先不简写
        personList() {
            return this.$store.state.personList
        },
        sum() {
            return this.$store.state.sum
        }
        //简写
        //...mapState(['personList'])
    },
    methods: {
        add() {
            const personObj = {
                id: nanoid(),
                name: this.name,
            }
            //通知vuex添加person
            this.$store.commit('ADD_PERSON', personObj)
            //清空输入框
            this.name = ''
        }
    }
}
</script>

<style scoped>
</style>